﻿/*****************************************************
**Copyright(C), 2021-2022, Ciqtek Technology.
**Version:     2.0
**Author:      hujiarui
**Date:        2023/06/28
**Description: 基类Item派生类，矩形
*****************************************************/

#ifndef RECTGRAPHICSITEM_H
#define RECTGRAPHICSITEM_H

#include "../../ofuidll_global.h"

#include "BaseGraphicsItem.h"

namespace OfUI {

	class OFUIDLL_EXPORT RectGraphicsItem : public BaseGraphicsItem
	{
		Q_OBJECT
			Q_INTERFACES(QGraphicsItem)

	public:
		/**
		 * @brief  选择类型
		 */
		enum SelectType
		{
			SELECT_NONE = 0,        ///< 未选择
			SELECT_TOP,             ///< 选择上
			SELECT_LEFT,            ///< 选择左
			SELECT_RIGHT,           ///< 选择右
			SELECT_BOTTOM,          ///< 选择下
			SELECT_CENTER,          ///< 选择中
			SELECT_TOP_LEFT,        ///< 选择左上
			SELECT_TOP_RIGHT,       ///< 选择右上
			SELECT_BOTTOM_LEFT,     ///< 选择左下
			SELECT_BOTTOM_RIGHT,    ///< 选择右下
			SELECT_ALL              ///< 选择整体
		};

	public:
		/**
		 * @brief  RectGraphicsItem 构造函数
		 * @param  parent
		 * @return
		 */
		RectGraphicsItem(QGraphicsItem *parent = 0);

		/**
		 * @brief  ~RectGraphicsItem 析构函数
		 * @return
		 */
		virtual ~RectGraphicsItem();

	public:
		/**
		 * @brief  setRect 设置矩形
		 * @param  rect
		 * @return void
		 */
		void setRect(const QRectF& rect);

		/**
		 * @brief  getRect 获取矩形
		 * @return QRectF
		 */
		QRectF getRect() const;

	public:
		/**
		 * @brief  isAdding 判断是否正在添加
		 * @return bool
		 */
		bool isAdding() const override;

		/**
		 * @brief  isEditing 判断是否正在编辑
		 * @return bool
		 */
		bool isEditing() const override;

		/**
		 * @brief  isVaild 判断是否有效
		 * @return bool
		 */
		bool isVaild() const override;

		/**
		 * @brief  select 坐标选择拖拽类型
		 * @param  point
		 * @param  selType
		 * @return BaseGraphicsItem::MouseDragType
		 */
		MouseDragType select(const QPointF& point, int* selType = nullptr) const override;

		/**
		 * @brief  cursor 坐标位置设置光标
		 * @param  point
		 * @param  cursor
		 * @return void
		 */
		void cursor(const QPointF& point, QCursor& cursor) const override;

		/**
		 * @brief  mousePressEvent 鼠标按下事件
		 * @param  event
		 * @return void
		 */
		void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

		/**
		 * @brief  mouseReleaseEvent 鼠标弹起事件
		 * @param  event
		 * @return void
		 */
		void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

		/**
		 * @brief  mouseMoveEvent 鼠标移动事件
		 * @param  event
		 * @return void
		 */
		void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

		/**
		 * @brief  focusOutEvent 失焦事件
		 * @param  event
		 * @return void
		 */
		void focusOutEvent(QFocusEvent *event) override;

	protected:
		/**
		 * @brief  boundingRect 重写boundingRect
		 * @return QRectF
		 */
		QRectF boundingRect() const override;

		/**
		 * @brief  paint 重写paint
		 * @param  painter
		 * @param  option
		 * @param  widget
		 * @return void
		 */
		void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = nullptr */) override;

	protected:
		/**
		 * @brief  getDragSelectPoint 获取拖拽点
		 * @param  rect
		 * @return QMap<SelectType, QPointF>
		 */
		QMap<SelectType, QPointF> getDragSelectPoint(const QRectF& rect) const;

	protected:
		/** 矩形区域
		 */
		QRectF m_rect;

		/** 是否正在添加
		 */
		bool m_bAdding;

		/** 选择类型
		 */
		SelectType m_eSelType;

		/** 鼠标按下位置
		 */
		QPointF m_pressMousePos;
	};

}

#endif // RECTGRAPHICSITEM_H
